# coding:utf-8



def to_json(obj):
    if obj is None or obj.__class__.__name__ in __builtins__:
        return obj
    else:
        value = {'__class__': obj.__class__.__name__}
        for k, v in list(obj.__dict__.items()):
            value[k] = to_json(v)
        return value


def from_json(obj, cls=dict):
    if isinstance(obj, dict):
        if '__class__' in obj:
            ret = cls()
            for k, v in list(obj.items()):
                if k != '__class__':
                    v = from_json(v)
                    setattr(ret, k, v)
        else:
            ret = {}
            for k, v in list(obj.items()):
                ret[k] = from_json(v)
        return ret
    elif isinstance(obj, list):
        return [from_json(v) for v in obj]
    else:
        return obj
